<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

class Region extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 国家简码 => 国家名称
     * 地区
     */
    public function index()
    {
        $type = $this->request->request('type');
        if($type == 1){
            $rows = Db::name('region_city')->group('country_code')->order('country','asc')->select();
            $data = [];
            foreach ($rows as $row)
            {
                $data[] = $row['country'];
            }
        }elseif($type == 2){
            $name = $this->request->request('name');
            $rows = Db::name('region_city')->where('country',$name)->where('state is not null')->group('state_code')->order('state','asc')->select();
            $data = [];
            if($rows){
                foreach ($rows as $row)
                {
                    $data[] = $row['state'];
                }
            }
        }elseif($type == 3){
            $name = $this->request->request('name');
            $rows = Db::name('region_city')->where('state',$name)->order('city','asc')->select();
            $data = [];
            if($rows){
                foreach ($rows as $row)
                {
                    $data[] = $row['city'];
                }
            }
        }else{
            $name = $this->request->request('name');
            $rows = Db::name('region_city')->where('country',$name)->order('city','asc')->select();
            $data = [];
            if($rows) {
                foreach ($rows as $row) {
                    $data[] = $row['city'];
                }
            }
        }

        $this->success('获取成功', $data);
    }

}